package socket;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

public class ServerTest1 {
    private ServerSocket serverSocket;
    //private PrintWriter[] allOut = {};
    private Collection<PrintWriter> allOut = new ArrayList<>();

    public ServerTest1(){
        try {
            System.out.println("正在启动服务器...");
            serverSocket = new ServerSocket(8088);
            System.out.println("服务器启动完毕！");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void start(){
        try {
            while(true){
                Socket socket = serverSocket.accept();
                System.out.println(socket.getInetAddress().getHostAddress()+"已连接");
                ClientHander hander = new ClientHander(socket);
                Thread t = new Thread(hander);
                t.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args){
        ServerTest1 server = new ServerTest1();
        server.start();
    }

    private class ClientHander implements Runnable{
        private Socket socket;
        private String host;

        public ClientHander(Socket socket){
            this.socket = socket;
            host = socket.getInetAddress().getHostAddress();
        }
        public void run() {
            PrintWriter pw = null;
            try {
                InputStream iss = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(iss, StandardCharsets.UTF_8);
                BufferedReader br = new BufferedReader(isr);

                OutputStream out = socket.getOutputStream();
                OutputStreamWriter osw = new OutputStreamWriter(out,StandardCharsets.UTF_8);
                BufferedWriter bw = new BufferedWriter(osw);
                pw = new PrintWriter(bw,true);
                synchronized (ServerTest1.this) {
                    allOut.add(pw);
                }
                message(host+"上线了,目前在线:"+allOut.size()+"人");
                String line;
                while ((line = br.readLine())!=null){
                    message(host+"说:"+line);
                }

            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    synchronized (ServerTest1.this) {
                        allOut.remove(pw);
                    }
                    message(host+"下线了,目前在线:"+allOut.size()+"人");
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        private void message(String message){
            System.out.println(message);
            synchronized (ServerTest1.this) {
                for (PrintWriter data : allOut) {
                    data.println(host + "说:" + message);
                }
            }
        }
    }
}

